import requests
import re
import json

def get_ip():
    header = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36'
    }
    res = requests.get("https://www.kuaidaili.com/free/",headers=header)

    ip_info = re.search(r'const fpsList = (.*?);', res.text, re.S).group(1)
    ips = json.loads(ip_info)
    for ip in ips:
        if ip["is_valid"]:
            print(ip)
            return f'{ip["ip"]}:{ip["port"]}'
    else:
        print(f"没有有效IP")


ip_port = get_ip()
if ip_port:
    proxies = {
        "http": "http://%(proxy)s/" % {'proxy': ip_port},
        "https": "http://%(proxy)s/" % {'proxy': ip_port}
    }

    res = requests.get("http://httpbin.org/get", proxies=proxies)
    print(res.json())
